package com.jinghao.ctws.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.jinghao.core.common.exception.JingHaoCustomerException;
import com.jinghao.core.common.service.impl.JingHaoBaseServiceImpl;
import com.jinghao.core.common.util.JhMD5Util;
import com.jinghao.core.common.util.cnd.JhCnd;
import com.jinghao.core.common.util.cnd.JhUpdate;
import com.jinghao.ctws.service.ICwvPublicService;
import com.jinghao.ctws.service.ICwvPublicSysService;
import com.jinghao.ctws.util.EJhCTwsCode;
import com.jinghao.sys.entity.SysAccount;
import com.jinghao.sys.entity.SysAccount.ESysAccount;
import com.jinghao.sys.service.ISysAccountService;
import com.jinghao.sys.util.EJhSysCode;

@Service("cwvPublicService")
public class CwvPublicServiceImpl extends JingHaoBaseServiceImpl implements ICwvPublicService {
	//==================定制内容开始======================
//在这里修改代码
	@Resource
	public ISysAccountService sysAccountService;
	
	@Resource
	public ICwvPublicSysService cwvPublicSysService;
	
	public void updateloginByUserAndPwd(SysAccount model){
		  if(StringUtils.isEmpty(model.getJhactCode())){
			   throw new JingHaoCustomerException(EJhSysCode.SYS4000);
		  }
		  if(StringUtils.isEmpty(model.getJhactPassword())){
			   throw new JingHaoCustomerException(EJhSysCode.SYS4000);
		  }
		  model.setJhactPassword(JhMD5Util.getMD5(model.getJhactPassword()));
		  JhCnd cnd=new JhCnd();
	      cnd.andEquals("jhactCode");
	      cnd.andEquals("jhactPassword");
	      cnd.andEquals("jhactNature");
	      model.setJhactNature(ESysAccount.jhactNature_0.getKey());
	      model.setJhCnd(cnd);
	      List<Map<String,Object>> listMap=this.sysAccountService.queryMapListById(model, "queryModelListAccountAndOrgByUserCode");
	      if(listMap.isEmpty()){
	    	  throw new JingHaoCustomerException(EJhSysCode.SYS4001);
	      }else{
	    	  if(listMap.size()>1){
	    		  throw new JingHaoCustomerException("账户绑多个机构，系统无法进行选择，请联系系统管理员！");
	    	  }
	    	  Map<String,Object> _map=listMap.get(0); 
	    	  model.setJhactAccessToken(model.getUUID());
	    	  model.setJhactUuid(_map.get("jhactUuid").toString());
	    	  Calendar nowTime = Calendar.getInstance();
			  nowTime.add(Calendar.MINUTE, 30);
	    	  model.setJhactTokenExpirationTime(nowTime.getTime());
	    	  
	    	  String jhactStatus=_map.get("jhactStatus")!=null?_map.get("jhactStatus").toString():"";
	    	  if(!jhactStatus.equals(ESysAccount.jhactStatus_1.getKey())){
	    		  throw new JingHaoCustomerException(EJhSysCode.SYS4003);
	    	  }
	    	  JhUpdate updateSet=new JhUpdate();
	    	  updateSet.add("jhactAccessToken");
	    	  updateSet.add("jhactTokenExpirationTime");
	    	  model.setJhUpdate(updateSet);
	    	  this.update(model);
	    	  
	    	  _map.put("accessToken", model.getJhactAccessToken());
	    	  _map.remove("jhactStatus");
	    	  _map.remove("jhactUuid");
	    	  _map.remove("jhactCode");
	    	  _map.remove("jhactName");
	    	  model.setMapList(listMap);
	      }
	}
	
	/**
	 * 检查用户是否登录 
	 * @param map
	 * @param model
	 * @return
	 */
	public SysAccount checkUserIsLoginInfo(String accessToken){
		if(accessToken==null||accessToken.equals("")){
			 throw new JingHaoCustomerException(EJhCTwsCode.TWS4001.getCode(),"请提供accessToken信息！");
		}
		Map<String,Object> newMap=new HashMap<String,Object>();
		newMap.put("accessToken", accessToken);
		SysAccount acc=new SysAccount();
		acc.setObjMap(newMap);
		this.cwvPublicSysService.updateCheckIsLoginByToken(acc);
		return acc;
	}
	//==================定制内容结束======================
}
